<?php
/**
 * 文件上传
 */

namespace app\admin\controller;


use app\common\controller\AdminController;
use app\common\lib\RespCode;
use custom\Redis;
use oss\Oss;
use think\Exception;
use think\facade\Env;
use think\facade\Request;

class Upload extends AdminController {

    protected $notNeedAuthAction = ['img'];

    function img() {
        try {
            $file = request()->file('file');
            $type = input('type');
            $video_ext = 'mp4,MPEG,baiAVI,nAVI,ASF,MOV,3GP,WMV,DivX,XviD,RM,RMVB,FLV,F4V';
            $img_ext = 'jpg,png,gif,jpeg';
            if (empty($type)) {
                throw new Exception('文件类型不能为空', RespCode::PARAMS_ERR);
            }
            if (!in_array($type, ['house_pic', 'house_village', 'app_logo', 'h5_logo', 'company_video', 'video_cover', 'company_applets_code', 'housestatistics', 'teacher_video', 'teacher_video_cover', 'wiki_cover', 'adver', 'system_message_pic', 'banner'])) {
                throw new Exception('错误的文件类型', RespCode::PARAMS_ERR);
            }
            if (empty($file)) {
                throw new Exception('请选择文件', RespCode::FILE_EMPTY);
            }
            //房源图片
            if ($type == 'house_pic') {
                if (!$file->check(['size' => 5242880, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //小区图片
            if ($type == 'house_village') {
                if (!$file->check(['size' => 5242880, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //公司app_logo
            if ($type == 'app_logo') {
                if (!$file->check(['size' => 1048576, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //公司h5_logo
            if ($type == 'h5_logo') {
                if (!$file->check(['size' => 1048576, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //公司视频
            if ($type == 'company_video') {
                if (!$file->check(['size' => 8388608, 'ext' => $video_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //公司视频封面
            if ($type == 'video_cover') {
                if (!$file->check(['size' => 5242880, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //公司小程序二维码
            if ($type == 'company_applets_code') {
                if (!$file->check(['size' => 5242880, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //成交情报
            if ($type == 'housestatistics') {
                if (!$file->check(['size' => 10485760, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //名师讲堂视频
            if ($type == 'teacher_video') {
                if (!$file->check(['size' => 52428800, 'ext' => $video_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //名师讲堂视频封面
            if ($type == 'teacher_video_cover') {
                if (!$file->check(['size' => 1048576, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //法拍百科封面
            if ($type == 'wiki_cover') {
                if (!$file->check(['size' => 1048576, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //开屏广告
            if ($type == 'adver') {
                if (!$file->check(['size' => 10485760, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            if ($type == 'system_message_pic') {
                if (!$file->check(['size' => 10485760, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            //轮播
            if ($type == 'banner') {
                if (!$file->check(['size' => 10485760, 'ext' => $img_ext])) {
                    throw new Exception($file->getError(),RespCode::PARAMS_ERR);
                }
            }
            $info = $file->getInfo();
            $oss = Oss::getInstance();
            if (Env::get('app.dev', 'dev') == 'dev') $dir = "test/admin/{$type}";
            else $dir = "admin/{$type}";
            $filename = $oss->createFilename($dir, get_ext($info['name']));
            if ($oss->uploadFile($info['tmp_name'])) {
                $redisModel = Redis::getInstance();
                $redisModel->hset('upload_file_admin', date('Y-m-d'), $type);
                return $this->resData('success', ['url' => $filename]);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
